import logging
from google.appengine.api import xmpp
from google.appengine.ext import db 
from google.appengine.ext import webapp 
from google.appengine.ext.webapp import xmpp_handlers 
from google.appengine.ext.webapp.util import run_wsgi_app

class User(db.Model):
  mail = db.StringProperty()
  type = db.StringProperty(choices=set(["weather","xiaonei"]))
  user = db.StringProperty()
  password = db.StringProperty()
  data = db.StringProperty(multiline=True)

class Weather(db.Model):
  city = db.StringProperty()
  data = db.TextProperty()

class WebHandler_send(webapp.RequestHandler):
  def get(self):
    users = User.all()
    users.filter("type =", "weather")
    #self.response.out.write("Daily weather send list<br>")
    for user in users:
      if xmpp.get_presence(user.mail):
        data = Weather.all().filter("city =", user.user).get()
        if (data):
          data = data.data
        else:
          data = 'City not register in system, please rebind the account'
        xmpp.send_message(user.mail, data)
        #self.response.out.write("%s %s %s<br>" % (user.mail, user.user, data))

class WebHandler_update(webapp.RequestHandler):
  def get(self):
    logging.info("Update begin")

def main():
  logging.getLogger().setLevel(logging.DEBUG)
  application = webapp.WSGIApplication(
      [
        ('/weather/daily_send', WebHandler_send),
        ('/weather/daily_update', WebHandler_update) 
        ],
      debug=True)
  run_wsgi_app(application)

if __name__ == "__main__":
  main();
